﻿/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.bourre.log.PixlibStringifier;
import com.bourre.transitions.IFrameListener;
import com.kairos.engine.RTEvent;
import com.kairos.engine.RTObject;
import com.kairos.motion.strategy.IPlayStrategy;
import com.kairos.motion.strategy.LoopStrategy;
import com.kairos.motion.strategy.NoLoopStrategy;
import com.kairos.motion.strategy.ReversedLoopStrategy;
import com.kairos.motion.strategy.ReversedNoLoopStrategy;
import com.kairos.motion.AbstractAnimation;

/**
 * {@code MovieClipRT} is dedicated to control {@code MovieClip} animations
 * in Kairos.
 * 
 * <p>With {@code MovieClipRT} you can run a {@code MovieClip} at the speed
 * you want, whatsoever the speed of the swf.</p>
 * 
 * @author  Cédric Néhémie	
 * @see		com.kairos.engine.RTBeacon
 * @see		com.kairos.engine.RTObject
 * @example // simply create a looping animation with MovieClipRT
 * var myAnim : MovieClip = _root.attachMovie( "Animation", "ttest", 2 );
 * 
 * var mch : MovieClipRT = new MovieClipRT ( myAnim, 24);
 * mch.execute(); 
 */
class com.kairos.motion.MovieClipRT extends AbstractAnimation implements IFrameListener
{
	private var _m : MovieClip;
	
	/**
	 * Create a new {@code MovieClipRT} witch handle the passed-in {@code MovieClip}.
	 * 
	 * <p>The animation is played at {@code fps} speed.</p>
	 * 
	 * <p>If {@code loop} argument is omitted then {@code loop} is set
	 * to {@code true}.</p>
	 * 
	 * <p>If {@code reverse} argument is omitted then {@code reverse}
	 * is set to {@code false}.</p>
	 * 
	 * <p>The {@code MovieClip} animation si stop at creation.</p>
	 * 
	 * @param   mc	The MovieClip to work with.    
	 * @param   fps The speed of the animation.
	 * @param   l   Does the animation loop ?
	 * @param	r	{@code true} to make the movieclip playing reversed. 
	 * @example	var mch : MovieClipRT = new MovieClipRT ( myMc, 40 );
	 * @example	var mch : MovieClipRT = new MovieClipRT ( myMc, 40, false);
	 * @example var mch : MovieClipRT = new MovieClipRT ( myMC, 40, true, true );
	 */
	public function MovieClipRT ( mc : MovieClip, fps : Number, l : Boolean, r : Boolean )
	{
		super( fps, l, r );
		setMovieClip( mc );
		reset();
	}
	
	
	/**
	 * Define a {@code MovieClip} to use with the {@code MovieClipRT}.
	 * 
	 * @param   mc	The target {@code MovieClip}.
	 */
	public function setMovieClip ( mc : MovieClip ) : Void
	{
		_m = mc;
		_tf = _m._totalframes;
		_f = _r ? 0 : _tf;
		_m.stop();
	}
	
	/**
	 * Return the current {@code MovieClipRT} target
	 * 
	 * @return  The target {@code MovieClip}.
	 */
	public function getMovieClip () : MovieClip 
	{
		return _m;
	}
	
	/**
	 * Starts playing the MovieClip animation at the specified frame. 
	 * 
	 * @param	f	A number representing the frame number,
	 * or a string representing the label of the frame,
	 * to which the playhead is sent.
	 */
	public function gotoAndPlay ( f ) : Void
	{
		_f = f;
		_frameChanged ( _f );
		super.start();
	}
	
	/**
	 * Brings the playhead to the specified frame of the movie clip
	 * and stops it there. 
	 * 
	 * @param	f	A number representing the frame number,
	 * or a string representing the label of the frame,
	 * to which the playhead is sent.
	 */
	public function gotoAndStop ( f : Number ) : Void
	{
		_f = f;
		_frameChanged ( _f );
		super.stop();
	}

	
	/**
	 * Make the composed {@code MovieClip} visible on the screen.
	 */
	public function show () : Void
	{
		_m._visible = true;
	}
	
	/**
	 * Make the composed {@code MovieClip} invisible.
	 */
	public function hide () : Void
	{
		_m._visible = false;
	} 
	
	/**
	 * Called when frame changed according to internal computation.
	 */
	 private function _frameChanged ( f : Number ) : Void
	 { 
	 	_m.gotoAndStop( f );
	 }
	 
	/**
	 * Return the String representation of the object. 
	 */	
	public function toString() : String
	{
		return PixlibStringifier.stringify( this );
	}
}